package ocean

import (
	"cid-media-api/internal/adcore"
	"context"
	"fmt"
	. "github.com/oceanengine/ad_open_sdk_go/models"
	"golang.org/x/time/rate"
)

type ProjectService struct {
	client      *OceanClient
	rateLimiter *rate.Limiter
	ctx         context.Context
}

func NewProjectService(client *OceanClient, limiter *rate.Limiter) *ProjectService {
	return &ProjectService{
		client:      client,
		rateLimiter: limiter,
		ctx:         context.Background(),
	}
}

func (s *ProjectService) BatchCreate(
	ctx context.Context,
	adGroups adcore.AdGroupParams,
) (projectID *int64, err error) {

	// 尝试获取请求许可
	if err := s.rateLimiter.Wait(ctx); err != nil {
		return nil, err
	}

	// 将 adcore.CampaignParams 转换为 ProjectCreateV30Request
	request := ProjectCreateV30Request{
		AdvertiserId: s.client.advertiserID,
		// 这里需要根据实际的 CampaignParams 字段来填充 ProjectCreateV30Request 的字段
		Name: adGroups.Name,
		// ... 其他必要的字段
	}

	// 执行项目创建请求
	resp, _, err := s.client.cli.ProjectCreateV30Api().
		Post(ctx).
		AccessToken(s.client.accessToken). // 需要替换为实际的访问令牌
		ProjectCreateV30Request(request).
		Execute()
	if err != nil {
		return nil, err
	}

	// 假设 resp 包含创建成功后的项目ID
	if resp.Data != nil {
		return resp.Data.ProjectId, nil

	} else {
		return nil, fmt.Errorf("创建项目响应数据为 nil")
	}
}
